package com.canvasgames.texture;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

public class Texture {
	
	private int id;
	private int parentId;
	private int width;
	private int height;
	private int offsetX;
	private int offsetY;
	
	private FloatBuffer verticesBuffer = null;
	private FloatBuffer textureBuffer; // New variable.
	private ShortBuffer indicesBuffer = null;

	public Texture() {
		
	}

	public Texture(int id, int parentId, int offsetX, int offsetY, int width, int height) {
		this.id = id;
		this.parentId = parentId;
		this.offsetX = offsetX;
		this.offsetY = offsetY;
		this.width = width;
		this.height = height;
		setVerticesBuffer(width, height);
		setTextureBuffer(offsetX, offsetY, width, height);
	}
	
	private void setVerticesBuffer(int width, int height) {
		float vertices[] = new float[]{
				0,     0,       0  ,
				width, 0,       0  , 
				width, height,  0  , 
				0,     height,  0
				} ;
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		verticesBuffer = vbb.asFloatBuffer();
		verticesBuffer.put(vertices);
		verticesBuffer.position(0);
	}
	
	private void setTextureBuffer(int offsetX, int offsetY, int width, int height) {
		float textureCoordinates[] = { 
				offsetX, offsetY, //
				offsetX + width, offsetY, //
				offsetX + width, offsetY + height, //
				offsetX, offsetY + height, // 
		};
		ByteBuffer byteBuf = ByteBuffer
		.allocateDirect(textureCoordinates.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		textureBuffer = byteBuf.asFloatBuffer();
		textureBuffer.put(textureCoordinates);
		textureBuffer.position(0);
	}
	
	
	
	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public int getParentId() {
		return parentId;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getWidth() {
		return width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getHeight() {
		return height;
	}

	public void setOffsetX(int offsetX) {
		this.offsetX = offsetX;
	}

	public int getOffsetX() {
		return offsetX;
	}

	public void setOffsetY(int offsetY) {
		this.offsetY = offsetY;
	}

	public int getOffsetY() {
		return offsetY;
	}


	public FloatBuffer getVerticesBuffer() {
		return verticesBuffer;
	}


	public FloatBuffer getTextureBuffer() {
		return textureBuffer;
	}


	public ShortBuffer getIndicesBuffer() {
		return indicesBuffer;
	}
	
	
}
